package com.jinzhending.controller.wx;

import com.jinzhending.entity.GoodsInfo;
import com.jinzhending.entity.WxSwiper;
import com.jinzhending.entity.WxUser;
import com.jinzhending.service.wx.GoodsListService;
import com.jinzhending.service.wx.IndexService;
import com.jinzhending.service.wx.UserService;
import com.jinzhending.utils.AESUtil;
import com.jinzhending.utils.JsonUtil;
import com.jinzhending.utils.JwtUtil;
import com.jinzhending.utils.RsaKeyProperties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: LieIdle
 * @Package: com.jinzhending.controller.wx  小程序首页
 * @Author: huat
 * @Date: 2020/4/3 16:47
 * @Version: 1.0
 */
@RestController
@RequestMapping("wx/index")
public class IndexController {

    @Autowired
    IndexService indexService;

    @Autowired
    RsaKeyProperties rsaKeyProperties;
    @Autowired
    UserService userService;
    @Autowired
    GoodsListService goodsListService;

    /**
     * 获取轮播图
     * @return
     */
    @RequestMapping("getAllSwiper")
    @Cacheable(cacheNames = "Swiper")
    public Map<String,Object> getAllSwiper(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",indexService.getAllSwiper());
        map.put("message","");
        return map;
    }

    /**
     * 获取登陆轮播图
     * @return
     */
    @RequestMapping("getAllSwiperLogin")
    @Cacheable(cacheNames = "SwiperLogin")
    public Map<String,Object> getAllSwiperLogin(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",indexService.getAllSwiperLogin());
        map.put("message","");
        return map;
    }

    /**
     * 获取商品列表
     * @return
     */
    @RequestMapping("getGoodsList")
    public Map<String,Object> getGoodsList(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",indexService.getGoods());
        map.put("message","");
        return map;
    }


    /**
     * 登陆
     * @param data 加密后的数据
     * @param response
     * @return
     */
    @RequestMapping("login")
    public Map<String,Object> login(String data, HttpServletResponse response){
        Map<String, Object> map = new HashMap<>();
        //解密数据并转换为实体类
        WxUser wxUser= JsonUtil.toBean(AESUtil.decrypt(data),WxUser.class);
        wxUser=userService.getWxUser(wxUser.getUsername(), wxUser.getPassword());
        if(null==wxUser){
            map.put("code", "500");
            map.put("message","账号密码错误");
            return map;
        }
        List<GoodsInfo> goodsInfoList = wxUser.getGoodsInfoList();
        String token= JwtUtil.generateTokenExpireInMinutes(wxUser,rsaKeyProperties.getPrivateKey(),24*60);
        if (null != token){
            response.setHeader("Authorization","bearer "+token);
            map.put("code", "200");
            map.put("message","认证成功");
            wxUser.setGoodsInfoList(goodsInfoList);
            map.put("data", AESUtil.encrypt(JsonUtil.ObjectToString(wxUser)));
            userService.updateTokenById(wxUser.getId(),token);
            return map;
        }else{
            map.put("code", "403");
            map.put("message","认证失败");
            return map;
        }
    }

    /**
     * 获取商品列表
     * @param startPage 起始页
     * @param pageSize  页面大小
     * @param cid 分类id
     * @param goodsName 商品名称
     * @return
     */
    @RequestMapping("getGoodList")
    public Object getGoodList(Integer startPage,Integer pageSize,Integer cid,String goodsName){
        if(null==startPage){
            startPage=1;
        }
        if(null==pageSize){
            pageSize=10;
        }
        if((null==goodsName||"".equals(goodsName))&&null==cid){
            return "400";
        }


        return goodsListService.getGoodList(startPage, pageSize,cid,goodsName);
    }

    /**
     * 获取商品信息通过id
     * @param goodsId 商品id
     * @return
     */
    @RequestMapping("getGoodInfoById")
    public GoodsInfo getGoodInfoById(Integer goodsId){
        if(null==goodsId){
            return null;
        }
        return goodsListService.getGoodById(goodsId);
    }

}
